#!/bin/bash

[% INCLUDE 'list_toolchain_updates-common-firefox-geckoview' %]

[% IF c("var/macos") %]
# macosx-sdk
read -d '' p << 'EOF' || true
sub l {
  ref $_[0] eq 'ARRAY' ? $_[0] : [ $_[0] ];
}

my $d = YAML::XS::LoadFile('taskcluster/ci/toolchain/macos-sdk.yml');

my $sdk_version;
foreach my $t (keys %$d) {
  foreach my $alias (@{l($d->{$t}{run}{'toolchain-alias'})}) {
    if ($alias eq 'macosx64-sdk') {
      if ($t =~ m/^macosx64-sdk-(.*)/) {
        print "$1";
        exit 0;
      }
    }
  }
}

exit 1;
EOF
needed=$(perl -MYAML::XS -e "$p")
current='[% pc("macosx-toolchain", "version") %]'
check_update_needed macos-sdk "$needed" "$current"

# cctools
read -d '' p << 'EOF' || true
my $d = YAML::XS::LoadFile('taskcluster/ci/fetch/toolchains.yml');
print $d->{'cctools-port'}{fetch}{revision};
EOF
needed=$(perl -MYAML::XS -e "$p")
current='[% pc("cctools", "git_hash") %]'
check_update_needed cctools "$needed" "$current"

# libtapi
read -d '' p << 'EOF' || true
my $d = YAML::XS::LoadFile('taskcluster/ci/fetch/toolchains.yml');
print $d->{'libtapi'}{fetch}{revision};
EOF
needed=$(perl -MYAML::XS -e "$p")
current='[% pc("libtapi", "git_hash") %]'
check_update_needed libtapi "$needed" "$current"

# End of macOS checks
[% END -%]


[% IF c("var/windows") %]
# mingw-w64
read -d '' p << 'EOF' || true
my $d = YAML::XS::LoadFile('taskcluster/ci/fetch/toolchains.yml');
print $d->{'mingw-w64'}{fetch}{revision};
EOF
needed=$(perl -MYAML::XS -e "$p")
current='[% pc("mingw-w64", "git_hash") %]'
check_update_needed mingw-w64 "$needed" "$current"


# fxc2
read -d '' p << 'EOF' || true
my $d = YAML::XS::LoadFile('taskcluster/ci/fetch/toolchains.yml');
print $d->{fxc2}{fetch}{revision};
EOF
needed=$(perl -MYAML::XS -e "$p")
current='[% pc("fxc2", "git_hash") %]'
check_update_needed fxc2 "$needed" "$current"

# End of Windows checks
[% END -%]


[% IF c("var/rlbox") %]
# wasi-sdk
read -d '' p << 'EOF' || true
my $d = YAML::XS::LoadFile('taskcluster/ci/fetch/toolchains.yml');
print $d->{'wasi-sdk'}{fetch}{revision};
EOF
needed=$(perl -MYAML::XS -e "$p")
current='[% pc("wasi-sysroot", "git_hash") %]'
check_update_needed wasi-sdk "$needed" "$current"

# End RLBox
[% END -%]
